<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<head>
<title>
    
                Caching
             &mdash; 
    Mako 0.8.1 Documentation
</title>

    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="_static/docs.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '#',
          VERSION:     '0.8.1',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>
        <script type="text/javascript" src="_static/jquery.js"></script>
        <script type="text/javascript" src="_static/underscore.js"></script>
        <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Mako 0.8.1 Documentation" href="index.html" />
        <link rel="prev" title="The Unicode Chapter" href="unicode.html" />

<link rel="stylesheet" href="_static/site.css"></link>


</head>
<body>
    <div id="wrap">
    <div class="rightbar">


    <div class="slogan">
    Hyperfast and lightweight templating for the Python platform.
    </div>


    </div>

    <a href="http://www.makotemplates.org/"><img src="_static/makoLogo.png" /></a>

    <hr/>

    









<div id="docs-container">



<div id="docs-header">
    <h1>Mako 0.8.1 Documentation</h1>

    <div id="docs-search">
    Search:
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>

    <div id="docs-version-header">
        Release: <span class="version-num">0.8.1</span>

    </div>

</div>

<div id="docs-top-navigation">
    <div id="docs-top-page-control" class="docs-navigation-links">
        <ul>
            <li>Prev:
            <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>
            </li>

        <li>
            <a href="index.html">Table of Contents</a> |
            <a href="genindex.html">Index</a>
            | <a href="_sources/caching.txt">view source
        </li>
        </ul>
    </div>

    <div id="docs-navigation-banner">
        <a href="index.html">Mako 0.8.1 Documentation</a>
        » 
                Caching
             

        <h2>
            
                Caching
            
        </h2>
    </div>

</div>

<div id="docs-body-container">

    <div id="docs-sidebar">
    <h3><a href="index.html">Table of Contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">Caching</a><ul>
<li><a class="reference internal" href="#cache-arguments">Cache Arguments</a><ul>
<li><a class="reference internal" href="#backend-specific-cache-arguments">Backend-Specific Cache Arguments</a></li>
<li><a class="reference internal" href="#using-the-beaker-cache-backend">Using the Beaker Cache Backend</a></li>
<li><a class="reference internal" href="#using-the-dogpile-cache-backend">Using the dogpile.cache Backend</a></li>
</ul>
</li>
<li><a class="reference internal" href="#programmatic-cache-access">Programmatic Cache Access</a></li>
<li><a class="reference internal" href="#cache-plugins">Cache Plugins</a><ul>
<li><a class="reference internal" href="#guidelines-for-writing-cache-plugins">Guidelines for Writing Cache Plugins</a></li>
</ul>
</li>
<li><a class="reference internal" href="#api-reference">API Reference</a></li>
</ul>
</li>
</ul>


    <h4>Previous Topic</h4>
    <p>
    <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>
    </p>

    <h4>Quick Search</h4>
    <p>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </p>

    </div>

    <div id="docs-body" class="withsidebar" >
        
<div class="section" id="caching">
<span id="caching-toplevel"></span><h1>Caching<a class="headerlink" href="#caching" title="Permalink to this headline">¶</a></h1>
<p>Any template or component can be cached using the <tt class="docutils literal"><span class="pre">cache</span></tt>
argument to the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> directives:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span><span class="cp">/&gt;</span><span class="x"></span>

<span class="x">template text</span>
</pre></div>
</div>
<p>The above template, after being executed the first time, will
store its content within a cache that by default is scoped
within memory. Subsequent calls to the template&#8217;s <a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><tt class="xref py py-meth docutils literal"><span class="pre">render()</span></tt></a>
method will return content directly from the cache. When the
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object itself falls out of scope, its corresponding
cache is garbage collected along with the template.</p>
<p>By default, caching requires that the <a class="reference external" href="http://beaker.readthedocs.org/">Beaker</a> package be installed on the
system, however the mechanism of caching can be customized to use
any third party or user defined system &#8211; see <a class="reference internal" href="#cache-plugins"><em>Cache Plugins</em></a>.</p>
<p>In addition to being available on the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tag, the caching flag and all
its options can be used with the <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tag as well:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">def</span> <span class="na">name=</span><span class="s">&quot;mycomp&quot;</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_timeout=</span><span class="s">&quot;60&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    other text</span>
<span class="cp">&lt;/%</span><span class="nb">def</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>... and equivalently with the <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> tag, anonymous or named:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_timeout=</span><span class="s">&quot;60&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    other text</span>
<span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<div class="section" id="cache-arguments">
<h2>Cache Arguments<a class="headerlink" href="#cache-arguments" title="Permalink to this headline">¶</a></h2>
<p>Mako has two cache arguments available on tags that are
available in all cases.   The rest of the arguments
available are specific to a backend.</p>
<p>The two generic tags arguments are:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">cached=&quot;True&quot;</span></tt> - enable caching for this <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>,
<tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>, or <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt>.</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">cache_key</span></tt> - the &#8220;key&#8221; used to uniquely identify this content
in the cache.   Usually, this key is chosen automatically
based on the name of the rendering callable (i.e. <tt class="docutils literal"><span class="pre">body</span></tt>
when used in <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>, the name of the def when using <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>,
the explicit or internally-generated name when using <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt>).
Using the <tt class="docutils literal"><span class="pre">cache_key</span></tt> parameter, the key can be overridden
using a fixed or programmatically generated value.</p>
<p>For example, here&#8217;s a page
that caches any page which inherits from it, based on the
filename of the calling template:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_key=</span><span class="s">&quot;${self.filename}&quot;</span><span class="cp">/&gt;</span><span class="x"></span>

<span class="cp">${</span><span class="nb">next</span><span class="o">.</span><span class="n">body</span><span class="p">()</span><span class="cp">}</span>

<span class="cp">## rest of template</span><span class="x"></span>
</pre></div>
</div>
</li>
</ul>
<p>On a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, the
caching can be configured using these arguments:</p>
<ul>
<li><p class="first"><tt class="docutils literal"><span class="pre">cache_enabled</span></tt> - Setting this
to <tt class="docutils literal"><span class="pre">False</span></tt> will disable all caching functionality
when the template renders.  Defaults to <tt class="docutils literal"><span class="pre">True</span></tt>.
e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span>
                <span class="n">directories</span><span class="o">=</span><span class="s">&#39;/path/to/templates&#39;</span><span class="p">,</span>
                <span class="n">cache_enabled</span> <span class="o">=</span> <span class="bp">False</span>
                <span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">cache_impl</span></tt> - The string name of the cache backend
to use.   This defaults to <tt class="docutils literal"><span class="pre">'beaker'</span></tt>, which has historically
been the only cache backend supported by Mako.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.6.0.</span></p>
<p>For example, here&#8217;s how to use the upcoming
<a class="reference external" href="http://dogpilecache.readthedocs.org">dogpile.cache</a>
backend:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span>
                <span class="n">directories</span><span class="o">=</span><span class="s">&#39;/path/to/templates&#39;</span><span class="p">,</span>
                <span class="n">cache_impl</span> <span class="o">=</span> <span class="s">&#39;dogpile.cache&#39;</span><span class="p">,</span>
                <span class="n">cache_args</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;regions&#39;</span><span class="p">:</span><span class="n">my_dogpile_regions</span><span class="p">}</span>
                <span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">cache_args</span></tt> - A dictionary of cache parameters that
will be consumed by the cache backend.   See
<a class="reference internal" href="#beaker-backend"><em>Using the Beaker Cache Backend</em></a> for examples.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.6.0.</span></p>
</li>
</ul>
<div class="section" id="backend-specific-cache-arguments">
<h3>Backend-Specific Cache Arguments<a class="headerlink" href="#backend-specific-cache-arguments" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>, and <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> tags
accept any named argument that starts with the prefix <tt class="docutils literal"><span class="pre">&quot;cache_&quot;</span></tt>.
Those arguments are then packaged up and passed along to the
underlying caching implementation, minus the <tt class="docutils literal"><span class="pre">&quot;cache_&quot;</span></tt> prefix.</p>
<p>The actual arguments understood are determined by the backend.</p>
<ul class="simple">
<li><a class="reference internal" href="#beaker-backend"><em>Using the Beaker Cache Backend</em></a> - Includes arguments understood by
Beaker.</li>
<li><a class="reference internal" href="#dogpile-cache-backend"><em>Using the dogpile.cache Backend</em></a> - Includes arguments understood by
dogpile.cache.</li>
</ul>
</div>
<div class="section" id="using-the-beaker-cache-backend">
<span id="beaker-backend"></span><h3>Using the Beaker Cache Backend<a class="headerlink" href="#using-the-beaker-cache-backend" title="Permalink to this headline">¶</a></h3>
<p>When using Beaker, new implementations will want to make usage
of <strong>cache regions</strong> so that cache configurations can be maintained
externally to templates.  These configurations live under
named &#8220;regions&#8221; that can be referred to within templates themselves.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.6.0: </span>Support for Beaker cache regions.</p>
<p>For example, suppose we would like two regions.  One is a &#8220;short term&#8221;
region that will store content in a memory-based dictionary,
expiring after 60 seconds.   The other is a Memcached region,
where values should expire in five minutes.   To configure
our <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>, first we get a handle to a
<a class="reference external" href="http://beaker.readthedocs.org/en/latest/modules/cache.html#beaker.cache.CacheManager" title="(in Beaker v1.6)"><tt class="xref py py-class docutils literal"><span class="pre">beaker.cache.CacheManager</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">beaker.cache</span> <span class="kn">import</span> <span class="n">CacheManager</span>

<span class="n">manager</span> <span class="o">=</span> <span class="n">CacheManager</span><span class="p">(</span><span class="n">cache_regions</span><span class="o">=</span><span class="p">{</span>
    <span class="s">&#39;short_term&#39;</span><span class="p">:{</span>
        <span class="s">&#39;type&#39;</span><span class="p">:</span> <span class="s">&#39;memory&#39;</span><span class="p">,</span>
        <span class="s">&#39;expire&#39;</span><span class="p">:</span> <span class="mi">60</span>
    <span class="p">},</span>
    <span class="s">&#39;long_term&#39;</span><span class="p">:{</span>
        <span class="s">&#39;type&#39;</span><span class="p">:</span> <span class="s">&#39;ext:memcached&#39;</span><span class="p">,</span>
        <span class="s">&#39;url&#39;</span><span class="p">:</span> <span class="s">&#39;127.0.0.1:11211&#39;</span><span class="p">,</span>
        <span class="s">&#39;expire&#39;</span><span class="p">:</span> <span class="mi">300</span>
    <span class="p">}</span>
<span class="p">})</span>

<span class="n">lookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span>
                <span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;/path/to/templates&#39;</span><span class="p">],</span>
                <span class="n">module_directory</span><span class="o">=</span><span class="s">&#39;/path/to/modules&#39;</span><span class="p">,</span>
                <span class="n">cache_impl</span><span class="o">=</span><span class="s">&#39;beaker&#39;</span><span class="p">,</span>
                <span class="n">cache_args</span><span class="o">=</span><span class="p">{</span>
                    <span class="s">&#39;manager&#39;</span><span class="p">:</span><span class="n">manager</span>
                <span class="p">}</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>Our templates can then opt to cache data in one of either region,
using the <tt class="docutils literal"><span class="pre">cache_region</span></tt> argument.   Such as using <tt class="docutils literal"><span class="pre">short_term</span></tt>
at the <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> level:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">page</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_region=</span><span class="s">&quot;short_term&quot;</span><span class="cp">&gt;</span>

<span class="cp">## ...</span><span class="x"></span>
</pre></div>
</div>
<p>Or, <tt class="docutils literal"><span class="pre">long_term</span></tt> at the <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt> level:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span><span class="nb">block</span> <span class="na">name=</span><span class="s">&quot;header&quot;</span> <span class="na">cached=</span><span class="s">&quot;True&quot;</span> <span class="na">cache_region=</span><span class="s">&quot;long_term&quot;</span><span class="cp">&gt;</span><span class="x"></span>
<span class="x">    other text</span>
<span class="cp">&lt;/%</span><span class="nb">block</span><span class="cp">&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>The Beaker backend also works without regions.   There are a
variety of arguments that can be passed to the <tt class="docutils literal"><span class="pre">cache_args</span></tt>
dictionary, which are also allowable in templates via the
<tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt>,
and <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> tags specific to those sections.   The values
given override those specified at the  <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>
or <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> level.</p>
<p>With the possible exception
of <tt class="docutils literal"><span class="pre">cache_timeout</span></tt>, these arguments are probably better off
staying at the template configuration level.  Each argument
specified as <tt class="docutils literal"><span class="pre">cache_XYZ</span></tt> in a template tag is specified
without the <tt class="docutils literal"><span class="pre">cache_</span></tt> prefix in the <tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">cache_timeout</span></tt> - number of seconds in which to invalidate the
cached data.  After this timeout, the content is re-generated
on the next call.  Available as <tt class="docutils literal"><span class="pre">timeout</span></tt> in the <tt class="docutils literal"><span class="pre">cache_args</span></tt>
dictionary.</li>
<li><tt class="docutils literal"><span class="pre">cache_type</span></tt> - type of caching. <tt class="docutils literal"><span class="pre">'memory'</span></tt>, <tt class="docutils literal"><span class="pre">'file'</span></tt>, <tt class="docutils literal"><span class="pre">'dbm'</span></tt>, or
<tt class="docutils literal"><span class="pre">'ext:memcached'</span></tt> (note that  the string <tt class="docutils literal"><span class="pre">memcached</span></tt> is
also accepted by the dogpile.cache Mako plugin, though not by Beaker itself).
Available as <tt class="docutils literal"><span class="pre">type</span></tt> in the <tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary.</li>
<li><tt class="docutils literal"><span class="pre">cache_url</span></tt> - (only used for <tt class="docutils literal"><span class="pre">memcached</span></tt> but required) a single
IP address or a semi-colon separated list of IP address of
memcache servers to use.  Available as <tt class="docutils literal"><span class="pre">url</span></tt> in the <tt class="docutils literal"><span class="pre">cache_args</span></tt>
dictionary.</li>
<li><tt class="docutils literal"><span class="pre">cache_dir</span></tt> - in the case of the <tt class="docutils literal"><span class="pre">'file'</span></tt> and <tt class="docutils literal"><span class="pre">'dbm'</span></tt> cache types,
this is the filesystem directory with which to store data
files. If this option is not present, the value of
<tt class="docutils literal"><span class="pre">module_directory</span></tt> is used (i.e. the directory where compiled
template modules are stored). If neither option is available
an exception is thrown.  Available as <tt class="docutils literal"><span class="pre">dir</span></tt> in the
<tt class="docutils literal"><span class="pre">cache_args</span></tt> dictionary.</li>
</ul>
</div>
<div class="section" id="using-the-dogpile-cache-backend">
<span id="dogpile-cache-backend"></span><h3>Using the dogpile.cache Backend<a class="headerlink" href="#using-the-dogpile-cache-backend" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="http://dogpilecache.readthedocs.org">dogpile.cache</a> is a new replacement for Beaker.   It provides
a modernized, slimmed down interface and is generally easier to use
than Beaker.   As of this writing it has not yet been released.  dogpile.cache
includes its own Mako cache plugin &#8211; see <a class="reference external" href="http://dogpilecache.readthedocs.org/en/latest/api.html#dogpile.cache.plugins.mako_cache" title="(in dogpile.cache v0.4.4)"><tt class="xref py py-mod docutils literal"><span class="pre">dogpile.cache.plugins.mako_cache</span></tt></a> in the
dogpile.cache documentation.</p>
</div>
</div>
<div class="section" id="programmatic-cache-access">
<h2>Programmatic Cache Access<a class="headerlink" href="#programmatic-cache-access" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>, as well as any template-derived <a class="reference internal" href="namespaces.html#mako.runtime.Namespace" title="mako.runtime.Namespace"><tt class="xref py py-class docutils literal"><span class="pre">Namespace</span></tt></a>, has
an accessor called <tt class="docutils literal"><span class="pre">cache</span></tt> which returns the <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> object
for that template. This object is a facade on top of the underlying
<a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> object, and provides some very rudimental
capabilities, such as the ability to get and put arbitrary
values:</p>
<div class="highlight-mako"><div class="highlight"><pre><span class="cp">&lt;%</span>
    <span class="n">local</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">&quot;somekey&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;memory&quot;</span><span class="p">,</span> <span class="s">&quot;somevalue&quot;</span><span class="p">)</span>
<span class="cp">%&gt;</span><span class="x"></span>
</pre></div>
</div>
<p>Above, the cache associated with the <tt class="docutils literal"><span class="pre">local</span></tt> namespace is
accessed and a key is placed within a memory cache.</p>
<p>More commonly, the <tt class="docutils literal"><span class="pre">cache</span></tt> object is used to invalidate cached
sections programmatically:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">template</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s">&#39;/sometemplate.html&#39;</span><span class="p">)</span>

<span class="c"># invalidate the &quot;body&quot; of the template</span>
<span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate_body</span><span class="p">()</span>

<span class="c"># invalidate an individual def</span>
<span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate_def</span><span class="p">(</span><span class="s">&#39;somedef&#39;</span><span class="p">)</span>

<span class="c"># invalidate an arbitrary key</span>
<span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">invalidate</span><span class="p">(</span><span class="s">&#39;somekey&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>You can access any special method or attribute of the <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a>
itself using the <a class="reference internal" href="#mako.cache.Cache.impl" title="mako.cache.Cache.impl"><tt class="xref py py-attr docutils literal"><span class="pre">impl</span></tt></a> attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">template</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">impl</span><span class="o">.</span><span class="n">do_something_special</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that using implementation-specific methods will mean you can&#8217;t
swap in a different kind of <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> implementation at a
later time.</p>
</div>
<div class="section" id="cache-plugins">
<span id="id1"></span><h2>Cache Plugins<a class="headerlink" href="#cache-plugins" title="Permalink to this headline">¶</a></h2>
<p>The mechanism used by caching can be plugged in
using a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> subclass.    This class implements
the rudimental methods Mako needs to implement the caching
API.   Mako includes the <a class="reference internal" href="#mako.ext.beaker_cache.BeakerCacheImpl" title="mako.ext.beaker_cache.BeakerCacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">BeakerCacheImpl</span></tt></a> class to
provide the default implementation.  A <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> class
is acquired by Mako using a <tt class="docutils literal"><span class="pre">pkg_resources</span></tt> entrypoint, using
the name given as the <tt class="docutils literal"><span class="pre">cache_impl</span></tt> argument to <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>
or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a>.    This entry point can be
installed via the standard <cite>setuptools</cite>/<tt class="docutils literal"><span class="pre">setup()</span></tt> procedure, underneath
the <cite>EntryPoint</cite> group named <tt class="docutils literal"><span class="pre">&quot;mako.cache&quot;</span></tt>.  It can also be
installed at runtime via a convenience installer <a class="reference internal" href="#mako.cache.register_plugin" title="mako.cache.register_plugin"><tt class="xref py py-func docutils literal"><span class="pre">register_plugin()</span></tt></a>
which accomplishes essentially the same task.</p>
<p>An example plugin that implements a local dictionary cache:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mako.cache</span> <span class="kn">import</span> <span class="n">Cacheimpl</span><span class="p">,</span> <span class="n">register_plugin</span>

<span class="k">class</span> <span class="nc">SimpleCacheImpl</span><span class="p">(</span><span class="n">CacheImpl</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cache</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">SimpleCacheImpl</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">cache</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">creation_function</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span> <span class="o">=</span> <span class="n">creation_function</span><span class="p">()</span>
            <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">invalidate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_cache</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>

<span class="c"># optional - register the class locally</span>
<span class="n">register_plugin</span><span class="p">(</span><span class="s">&quot;simple&quot;</span><span class="p">,</span> <span class="n">__name__</span><span class="p">,</span> <span class="s">&quot;SimpleCacheImpl&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Enabling the above plugin in a template would look like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s">&quot;mytemplate&quot;</span><span class="p">,</span>
             <span class="nb">file</span><span class="o">=</span><span class="s">&quot;mytemplate.html&quot;</span><span class="p">,</span>
             <span class="n">cache_impl</span><span class="o">=</span><span class="s">&#39;simple&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="guidelines-for-writing-cache-plugins">
<h3>Guidelines for Writing Cache Plugins<a class="headerlink" href="#guidelines-for-writing-cache-plugins" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>The <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> is created on a per-<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> basis.  The
class should ensure that only data for the parent <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> is
persisted or returned by the cache methods.    The actual <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>
is available via the <tt class="docutils literal"><span class="pre">self.cache.template</span></tt> attribute.   The <tt class="docutils literal"><span class="pre">self.cache.id</span></tt>
attribute, which is essentially the unique modulename of the template, is
a good value to use in order to represent a unique namespace of keys specific
to the template.</li>
<li>Templates only use the <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></tt></a> method
in an implicit fashion.  The <a class="reference internal" href="#mako.cache.CacheImpl.set" title="mako.cache.CacheImpl.set"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.set()</span></tt></a>,
<a class="reference internal" href="#mako.cache.CacheImpl.get" title="mako.cache.CacheImpl.get"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get()</span></tt></a>, and <a class="reference internal" href="#mako.cache.CacheImpl.invalidate" title="mako.cache.CacheImpl.invalidate"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.invalidate()</span></tt></a> methods are
only used in response to direct programmatic access to the corresponding
methods on the <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> object.</li>
<li><a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> will be accessed in a multithreaded fashion if the
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> itself is used multithreaded.  Care should be taken
to ensure caching implementations are threadsafe.</li>
<li>A library like <a class="reference external" href="http://pypi.python.org/pypi/dogpile.core">Dogpile</a>, which
is a minimal locking system derived from Beaker, can be used to help
implement the <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></tt></a> method in a threadsafe
way that can maximize effectiveness across multiple threads as well
as processes. <a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><tt class="xref py py-meth docutils literal"><span class="pre">CacheImpl.get_or_create()</span></tt></a> is the
key method used by templates.</li>
<li>All arguments passed to <tt class="docutils literal"><span class="pre">**kw</span></tt> come directly from the parameters
inside the <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;%block&gt;</span></tt>, or <tt class="docutils literal"><span class="pre">&lt;%page&gt;</span></tt> tags directly,
minus the <tt class="docutils literal"><span class="pre">&quot;cache_&quot;</span></tt> prefix, as strings, with the exception of
the argument <tt class="docutils literal"><span class="pre">cache_timeout</span></tt>, which is passed to the plugin
as the name <tt class="docutils literal"><span class="pre">timeout</span></tt> with the value converted to an integer.
Arguments present in <tt class="docutils literal"><span class="pre">cache_args</span></tt> on <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> or
<a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> are passed directly, but are superseded
by those present in the most specific template tag.</li>
<li>The directory where <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> places module files can
be acquired using the accessor <tt class="docutils literal"><span class="pre">self.cache.template.module_directory</span></tt>.
This directory can be a good place to throw cache-related work
files, underneath a prefix like <tt class="docutils literal"><span class="pre">_my_cache_work</span></tt> so that name
conflicts with generated modules don&#8217;t occur.</li>
</ul>
</div>
</div>
<div class="section" id="api-reference">
<h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="mako.cache.Cache">
<em class="property">class </em><tt class="descclassname">mako.cache.</tt><tt class="descname">Cache</tt><big>(</big><em>template</em>, <em>*args</em><big>)</big><a class="headerlink" href="#mako.cache.Cache" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
<p>Represents a data content cache made available to the module
space of a specific <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.6: </span><a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> by itself is mostly a
container for a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> object, which implements
a fixed API to provide caching services; specific subclasses exist to
implement different
caching strategies.   Mako includes a backend that works with
the Beaker caching system.   Beaker itself then supports
a number of backends (i.e. file, memory, memcached, etc.)</p>
<p>The construction of a <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> is part of the mechanics
of a <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>, and programmatic access to this
cache is typically via the <tt class="xref py py-attr docutils literal"><span class="pre">Template.cache</span></tt> attribute.</p>
<dl class="method">
<dt id="mako.cache.Cache.get">
<tt class="descname">get</tt><big>(</big><em>key</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve a value from the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.  The
backend is configured using these arguments upon first request.
Subsequent requests that use the same series of configuration
values will use that same backend.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.get_or_create">
<tt class="descname">get_or_create</tt><big>(</big><em>key</em>, <em>creation_function</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.get_or_create" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve a value from the cache, using the given creation function
to generate a new value.</p>
</dd></dl>

<dl class="attribute">
<dt id="mako.cache.Cache.id">
<tt class="descname">id</tt><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.id" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the &#8216;id&#8217; that identifies this cache.</p>
<p>This is a value that should be globally unique to the
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> associated with this cache, and can
be used by a caching system to name a local container
for data specific to this template.</p>
</dd></dl>

<dl class="attribute">
<dt id="mako.cache.Cache.impl">
<tt class="descname">impl</tt><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.impl" title="Permalink to this definition">¶</a></dt>
<dd><p>Provide the <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> in use by this <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a>.</p>
<p>This accessor allows a <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> with additional
methods beyond that of <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a> to be used programmatically.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.invalidate">
<tt class="descname">invalidate</tt><big>(</big><em>key</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.invalidate" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidate a value in the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.  The
backend is configured using these arguments upon first request.
Subsequent requests that use the same series of configuration
values will use that same backend.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.invalidate_body">
<tt class="descname">invalidate_body</tt><big>(</big><big>)</big><a class="headerlink" href="#mako.cache.Cache.invalidate_body" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidate the cached content of the &#8220;body&#8221; method for this
template.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.invalidate_closure">
<tt class="descname">invalidate_closure</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.invalidate_closure" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidate a nested <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> within this template.</p>
<p>Caching of nested defs is a blunt tool as there is no
management of scope &#8211; nested defs that use cache tags
need to have names unique of all other nested defs in the
template, else their content will be overwritten by
each other.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.invalidate_def">
<tt class="descname">invalidate_def</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.invalidate_def" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidate the cached content of a particular <tt class="docutils literal"><span class="pre">&lt;%def&gt;</span></tt> within this
template.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.put">
<tt class="descname">put</tt><big>(</big><em>key</em>, <em>value</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.put" title="Permalink to this definition">¶</a></dt>
<dd><p>A synonym for <a class="reference internal" href="#mako.cache.Cache.set" title="mako.cache.Cache.set"><tt class="xref py py-meth docutils literal"><span class="pre">Cache.set()</span></tt></a>.</p>
<p>This is here for backwards compatibility.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.Cache.set">
<tt class="descname">set</tt><big>(</big><em>key</em>, <em>value</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.Cache.set" title="Permalink to this definition">¶</a></dt>
<dd><p>Place a value in the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>value</strong> &#8211; the value.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="mako.cache.Cache.starttime">
<tt class="descname">starttime</tt><em class="property"> = None</em><a class="headerlink" href="#mako.cache.Cache.starttime" title="Permalink to this definition">¶</a></dt>
<dd><p>Epochal time value for when the owning <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> was
first compiled.</p>
<p>A cache implementation may wish to invalidate data earlier than
this timestamp; this has the effect of the cache for a specific
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> starting clean any time the <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a>
is recompiled, such as when the original template file changed on
the filesystem.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="mako.cache.CacheImpl">
<em class="property">class </em><tt class="descclassname">mako.cache.</tt><tt class="descname">CacheImpl</tt><big>(</big><em>cache</em><big>)</big><a class="headerlink" href="#mako.cache.CacheImpl" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
<p>Provide a cache implementation for use by <a class="reference internal" href="#mako.cache.Cache" title="mako.cache.Cache"><tt class="xref py py-class docutils literal"><span class="pre">Cache</span></tt></a>.</p>
<dl class="method">
<dt id="mako.cache.CacheImpl.get">
<tt class="descname">get</tt><big>(</big><em>key</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.CacheImpl.get" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve a value from the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="mako.cache.CacheImpl.get_or_create">
<tt class="descname">get_or_create</tt><big>(</big><em>key</em>, <em>creation_function</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.CacheImpl.get_or_create" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve a value from the cache, using the given creation function
to generate a new value.</p>
<p>This function <em>must</em> return a value, either from
the cache, or via the given creation function.
If the creation function is called, the newly
created value should be populated into the cache
under the given key before being returned.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>creation_function</strong> &#8211; function that when called generates
a new value.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="mako.cache.CacheImpl.invalidate">
<tt class="descname">invalidate</tt><big>(</big><em>key</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.CacheImpl.invalidate" title="Permalink to this definition">¶</a></dt>
<dd><p>Invalidate a value in the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="mako.cache.CacheImpl.pass_context">
<tt class="descname">pass_context</tt><em class="property"> = False</em><a class="headerlink" href="#mako.cache.CacheImpl.pass_context" title="Permalink to this definition">¶</a></dt>
<dd><p>If <tt class="docutils literal"><span class="pre">True</span></tt>, the <a class="reference internal" href="runtime.html#mako.runtime.Context" title="mako.runtime.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> will be passed to
<a class="reference internal" href="#mako.cache.CacheImpl.get_or_create" title="mako.cache.CacheImpl.get_or_create"><tt class="xref py py-meth docutils literal"><span class="pre">get_or_create</span></tt></a> as the name <tt class="docutils literal"><span class="pre">'context'</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="mako.cache.CacheImpl.set">
<tt class="descname">set</tt><big>(</big><em>key</em>, <em>value</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#mako.cache.CacheImpl.set" title="Permalink to this definition">¶</a></dt>
<dd><p>Place a value in the cache.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>key</strong> &#8211; the value&#8217;s key.</li>
<li><strong>value</strong> &#8211; the value.</li>
<li><strong>**kw</strong> &#8211; cache configuration arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="mako.cache.register_plugin">
<tt class="descclassname">mako.cache.</tt><tt class="descname">register_plugin</tt><big>(</big><em>self</em>, <em>name</em>, <em>modulepath</em>, <em>objname</em><big>)</big><a class="headerlink" href="#mako.cache.register_plugin" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="class">
<dt id="mako.ext.beaker_cache.BeakerCacheImpl">
<em class="property">class </em><tt class="descclassname">mako.ext.beaker_cache.</tt><tt class="descname">BeakerCacheImpl</tt><big>(</big><em>cache</em><big>)</big><a class="headerlink" href="#mako.ext.beaker_cache.BeakerCacheImpl" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">mako.cache.CacheImpl</span></tt></a></p>
<p>A <a class="reference internal" href="#mako.cache.CacheImpl" title="mako.cache.CacheImpl"><tt class="xref py py-class docutils literal"><span class="pre">CacheImpl</span></tt></a> provided for the Beaker caching system.</p>
<p>This plugin is used by default, based on the default
value of <tt class="docutils literal"><span class="pre">'beaker'</span></tt> for the <tt class="docutils literal"><span class="pre">cache_impl</span></tt> parameter of the
<a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><tt class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></tt></a> classes.</p>
</dd></dl>

</div>
</div>

    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links">
        Previous:
        <a href="unicode.html" title="previous chapter">The Unicode Chapter</a>

    <div id="docs-copyright">
        &copy; Copyright the Mako authors and contributors.
        Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3 
        with Mako templates.
    </div>
</div>

</div>

<div class="clearfix">

<hr/>

<div class="copyright">Website content copyright &copy; by Michael Bayer.
    All rights reserved.  Mako and its documentation are licensed
    under the MIT license.  mike(&)zzzcomputing.com</div>

</div>
</div>
</body>
</html>
